home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / include / pi-macros.h < prev    next >
C/C++ Source or Header  |  1997-07-12  |  8KB  |  282 lines

  1. #ifndef _PILOT_MACROS_H_
  2. #define _PILOT_MACROS_H_
  3.  
  4. #include "pi-args.h"
  5.  
  6. typedef unsigned long recordid_t;
  7.  
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11.  
  12. extern double get_float PI_ARGS((void *));
  13. extern void set_float PI_ARGS((void *, double));
  14. extern int compareTm PI_ARGS((struct tm *a, struct tm *b));
  15.  
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19.  
  20. #ifndef __cplusplus
  21.  
  22. #define get_long(ptr) ((unsigned long)\
  23.                        ((((unsigned char*)(ptr))[0] << 24) | \
  24.                        (((unsigned char*)(ptr))[1] << 16) | \
  25.                        (((unsigned char*)(ptr))[2] << 8)  | \
  26.                        (((unsigned char*)(ptr))[3])))
  27.  
  28. #define get_treble(ptr) ((unsigned long)\
  29.                         ((((unsigned char*)(ptr))[0] << 16) | \
  30.                          (((unsigned char*)(ptr))[1] << 8)  | \
  31.                          (((unsigned char*)(ptr))[2])))
  32.                        
  33. #define get_short(ptr) ((unsigned short)\
  34.                        ((((unsigned char*)(ptr))[0] << 8)  | \
  35.                         (((unsigned char*)(ptr))[1])))
  36.                         
  37. #define get_byte(ptr) (((unsigned char*)(ptr))[0])
  38.  
  39. #define get_slong(ptr) (signed long)(\
  40.                 (get_long((ptr)) > 0x7FFFFFFF) ?\
  41.                                 (((signed long)(get_long((ptr)) & 0x7FFFFFFF)) - 0x80000000):\
  42.                                 ((signed long)(get_long((ptr))))\
  43.                                 )
  44.  
  45. #define get_streble(ptr) (signed long)(\
  46.                 (get_treble((ptr)) > 0x7FFFFF) ?\
  47.                                 (((signed long)(get_treble((ptr)) & 0x7FFFFF)) - 0x800000):\
  48.                                 ((signed long)(get_treble((ptr))))\
  49.                                 )
  50.  
  51. #define get_sshort(ptr) (signed short)(\
  52.                 (get_short((ptr)) > 0x7FFF) ?\
  53.                                 (((signed short)(get_short((ptr)) & 0x7FFF)) - 0x8000):\
  54.                                 ((signed short)(get_short((ptr))))\
  55.                                 )
  56.  
  57. #define get_sbyte(ptr) (signed char)(\
  58.                 (get_byte((ptr)) > 0x7F) ?\
  59.                                 (((signed char)(get_byte((ptr)) & 0x7F)) - 0x80):\
  60.                                 ((signed char)(get_byte((ptr))))\
  61.                                 )
  62.  
  63. #define set_long(ptr,val) ((((unsigned char*)(ptr))[0] = (((unsigned long)(val)) >> 24) & 0xff), \
  64.                   (((unsigned char*)(ptr))[1] = (((unsigned long)(val)) >> 16) & 0xff), \
  65.                   (((unsigned char*)(ptr))[2] = (((unsigned long)(val)) >> 8) & 0xff), \
  66.                   (((unsigned char*)(ptr))[3] = (((unsigned long)(val)) >> 0) & 0xff))
  67.  
  68. #define set_slong(ptr,val) set_long((ptr),((unsigned long)(\
  69.                             (((signed long)(val)) < 0) ?\
  70.                             ((unsigned long)(((signed long)(val)) + 0x80000000) | 0x80000000) :\
  71.                             (val)\
  72.                            )))
  73.  
  74. #define set_treble(ptr,val) ((((unsigned char*)(ptr))[0] = (((unsigned long)(val)) >> 16) & 0xff), \
  75.                      (((unsigned char*)(ptr))[1] = (((unsigned long)(val)) >> 8) & 0xff), \
  76.                      (((unsigned char*)(ptr))[2] = (((unsigned long)(val)) >> 0) & 0xff))
  77.  
  78. #define set_streble(ptr,val) set_treble((ptr),((unsigned long)(\
  79.                             (((signed long)(val)) < 0) ?\
  80.                             ((unsigned long)(((signed long)(val)) + 0x800000) | 0x800000) :\
  81.                             (val)\
  82.                            )))
  83.                        
  84. #define set_short(ptr,val) ((((unsigned char*)(ptr))[0] = (((unsigned short)(val)) >> 8) & 0xff), \
  85.                     (((unsigned char*)(ptr))[1] = (((unsigned short)(val)) >> 0) & 0xff))
  86.  
  87. #define set_sshort(ptr,val) set_short((ptr),((unsigned short)(\
  88.                             (((signed short)(val)) < 0) ?\
  89.                             ((unsigned short)(((signed short)(val)) + 0x8000) | 0x8000) :\
  90.                             (val)\
  91.                            )))
  92.  
  93. #define set_byte(ptr,val) (((unsigned char*)(ptr))[0]=(val))
  94.  
  95. #define set_sbyte(ptr,val) set_byte((ptr),((unsigned char)(\
  96.                             (((signed char)(val)) < 0) ?\
  97.                             ((unsigned char)(((signed char)(val)) + 0x80) | 0x80) :\
  98.                             (val)\
  99.                            )))
  100.  
  101. #define char4(c1,c2,c3,c4) (((c1)<<24)|((c2)<<16)|((c3)<<8)|(c4))
  102.  
  103. #else /*ifdef __cplusplus*/
  104.  
  105. inline unsigned long get_long(const void *buf) 
  106. {
  107.      unsigned char *ptr = (unsigned char *) buf;
  108.  
  109.      return (*ptr << 24) | (*(++ptr) << 16) | (*(++ptr) << 8) | *(++ptr);
  110. }
  111.  
  112. inline signed long get_slong(const void *buf)
  113. {
  114.      unsigned long val = get_long(buf);
  115.      if (val > 0x7FFFFFFF)
  116.          return ((signed long)(val & 0x7FFFFFFF)) - 0x80000000;
  117.      else
  118.          return val;
  119. }
  120.  
  121. inline unsigned long get_treble(const void *buf) 
  122. {
  123.      unsigned char *ptr = (unsigned char *) buf;
  124.  
  125.      return (*ptr << 16) | (*(++ptr) << 8) | *(++ptr);
  126. }
  127.  
  128. inline signed long get_streble(const void *buf)
  129. {
  130.      unsigned long val = get_treble(buf);
  131.      if (val > 0x7FFFFF)
  132.          return ((signed long)(val & 0x7FFFFF)) - 0x800000;
  133.      else
  134.          return val;
  135. }
  136.  
  137. inline int get_short(const void *buf) 
  138. {
  139.      unsigned char *ptr = (unsigned char *) buf;
  140.  
  141.      return (*ptr << 8) | *(++ptr);
  142. }
  143.  
  144. inline signed short get_sshort(const void *buf)
  145. {
  146.      unsigned short val = get_short(buf);
  147.      if (val > 0x7FFF)
  148.          return ((signed short)(val & 0x7FFF)) - 0x8000;
  149.      else
  150.          return val;
  151. }
  152.  
  153. inline int get_byte(const void *buf) 
  154. {
  155.      return *((unsigned char *) buf);
  156. }
  157.  
  158. inline signed char get_sbyte(const void *buf)
  159. {
  160.      unsigned char val = get_byte(buf);
  161.      if (val > 0x7F)
  162.          return ((signed char)(val & 0x7F)) - 0x80;
  163.      else
  164.          return val;
  165. }
  166.  
  167. inline void set_long(void *buf, const unsigned long val) 
  168. {
  169.      unsigned char *ptr = (unsigned char *) buf;
  170.  
  171.      *ptr = (val >> 24) & 0xff;
  172.      *(++ptr) = (val >> 16) & 0xff;
  173.      *(++ptr) = (val >> 8) & 0xff;
  174.      *(++ptr) = val & 0xff;
  175. }
  176.  
  177. inline void set_slong(void *buf, const signed long val) 
  178. {
  179.      unsigned long uval;
  180.      
  181.      if (val < 0) {
  182.          uval = (val + 0x80000000);
  183.          uval |= 0x80000000;
  184.      } else
  185.          uval = val;
  186.      set_long(buf, uval);
  187. }
  188.  
  189. inline void set_treble(void *buf, const unsigned long val) 
  190. {
  191.      unsigned char *ptr = (unsigned char *) buf;
  192.      
  193.      *ptr = (val >> 16) & 0xff;
  194.      *(++ptr) = (val >> 8) & 0xff;
  195.      *(++ptr) = val & 0xff;
  196. }
  197.  
  198. inline void set_streble(void *buf, const signed long val) 
  199. {
  200.      unsigned long uval;
  201.      
  202.      if (val < 0) {
  203.          uval = (val + 0x800000);
  204.          uval |= 0x800000;
  205.      } else
  206.          uval = val;
  207.      set_treble(buf, uval);
  208. }
  209.  
  210. inline void set_short(void *buf, const int val) 
  211. {
  212.      unsigned char *ptr = (unsigned char *) buf;
  213.  
  214.      *ptr = (val >> 8) & 0xff;
  215.      *(++ptr) = val & 0xff;
  216. }
  217.  
  218. inline void set_sshort(void *buf, const signed short val) 
  219. {
  220.      unsigned short uval;
  221.      
  222.      if (val < 0) {
  223.          uval = (val + 0x8000);
  224.          uval |= 0x8000;
  225.      } else
  226.          uval = val;
  227.      set_treble(buf, uval);
  228. }
  229.  
  230. inline void set_byte(void *buf, const int val) 
  231. {
  232.      *((unsigned char *)buf) = val;
  233. }
  234.  
  235. inline void set_sbyte(void *buf, const signed char val) 
  236. {
  237.      unsigned char uval;
  238.      
  239.      if (val < 0) {
  240.          uval = (val + 0x80);
  241.          uval |= 0x80;
  242.      } else
  243.          uval = val;
  244.      set_byte(buf, uval);
  245. }
  246.  
  247. inline struct tm *getBufTm(struct tm *t, const void *buf, int setTime) 
  248. {
  249.      unsigned short int d = get_short(buf);
  250.      
  251.      t->tm_year = (d >> 9) + 4;
  252.      t->tm_mon = ((d >> 5) & 15) - 1;
  253.      t->tm_mday = d & 31;
  254.  
  255.      if (setTime) {
  256.       t->tm_hour = 0;
  257.       t->tm_min = 0;
  258.       t->tm_sec = 0;
  259.      }
  260.      
  261.      t->tm_isdst = -1;
  262.  
  263.      mktime(t);
  264.      
  265.      return t;
  266. }
  267.  
  268. inline void setBufTm(void *buf, const struct tm *t)
  269. {
  270.      set_short(buf,
  271.            ((t->tm_year - 4) << 9) | ((t->tm_mon + 1) << 5) | t->tm_mday);
  272. }
  273.  
  274. inline unsigned long char4(char c1, char c2, char c3, char c4)
  275. {
  276.      return (c1<<24)|(c2<<16)|(c3<<8)|c4;
  277. }
  278.  
  279. #endif /*__cplusplus*/
  280.  
  281. #endif /* _PILOT_MACROS_H_ */
  282.